import Cookies from 'js-cookie';
import config from './config';
import type { IUserInfo } from '@/api/index.d';

/**
 * 获取token
 */
export function getToken() {
  return Cookies.get(`${config.COOKIE_BEFORE}__${config.COOKIE_NAME}`) || '';
}

/**
 * 设置token
 */
export function setToken(value: string, expires?: number) {
  return Cookies.set(`${config.COOKIE_BEFORE}__${config.COOKIE_NAME}`, value, { expires });
}

/**
 * 删除token
 */
export function removeToken() {
  return Cookies.remove(`${config.COOKIE_BEFORE}__${config.COOKIE_NAME}`);
}

/**
 * 获取用户信息
*/
export function getUserInfo() {
  const value: string | undefined = Cookies.get(`${config.COOKIE_BEFORE}__userInfo`);
  return value ? JSON.parse(value) : null;
}

/**
 * 设置用户信息
*/
export function setUserInfo(value: IUserInfo) {
  return Cookies.set(`${config.COOKIE_BEFORE}__userInfo`, value);
}

/**
 * 删除用户信息
 */
export function removeUserInfo() {
  return Cookies.remove(`${config.COOKIE_BEFORE}__userInfo`);
}
